using System;
using System.Diagnostics;
using CSharpKit.Native.ECCODES;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace csk.ntv.grib
{
    [TestClass]
    public class UnitTest_GRIB_product_kind
    {
        [TestMethod]
        public void product_kind()
        {
            Assert.IsFalse(false);

            var fname = "d:/sfxData/ECMWF/eccodes_test_data/era5-levels-members.grib";
            // var fin = eccodes.file_open_read(fname);
            var fin = eccodes.codes_fopen(fname, FileOpenModel.Read);

            int cnt = 0;
            IntPtr h = IntPtr.Zero;
            while ((h = eccodes.codes_handle_new_from_file(IntPtr.Zero, fin, ProductKind.ANY, out int err)) != IntPtr.Zero || err != 0)
            {
                if (h == IntPtr.Zero)
                {
                    // fprintf(stderr, "Error: unable to create handle for message %d\n", cnt);
                    Debug.WriteLine("Error: unable to create handle for message %d\n", cnt);
                    cnt++;
                    continue;
                }
                // Debug.WriteLine(string.Format("message:{0}\n", cnt));
                Debug.WriteLine(string.Format("message: {0}\n", cnt));

                // CODES_CHECK(codes_get_length(h, "kindOfProduct", &len), 0);
                Assert.IsTrue(eccodes.codes_get_length(h, "kindOfProduct", out int len) == 0);
                // kind = (char*)malloc(len * sizeof(char));
                // var kind = new byte[len * Marshal.SizeOf<Byte>()];
                var ret = eccodes.codes_get_string(h, "kindOfProduct", out string kind);
                // var skind = BytesConverter.Instance.ToString(kind);


                Debug.WriteLine(string.Format("  product: {0}", kind));

                eccodes.codes_handle_delete(h);
                cnt++;
            }

            // 关闭文件句柄
            var stats = eccodes.codes_fclose(fin);

            Assert.IsFalse(false);
        }



        // {{END}}
    }



}

